#include "helpers.h"

namespace acidone
{

CategoryManager::CategoryManager(const Database *database)
	:m_database(database)
{
	for(int i = 0; i < EmbeddedCategoryCount; ++i)
		m_embeddedCategories.append(0);
}

void CategoryManager::setEmbeddedCategory(EmbeddedCategory type, Category *category)
{
	m_embeddedCategories[type] = category;
}

Category* CategoryManager::embeddedCategory(EmbeddedCategory type) const
{
	Q_ASSERT(type >= In && type <= LastCategory);

	return m_embeddedCategories[type];
}

bool CategoryManager::isEmbeddedCategory(Category *category) const
{
	return m_embeddedCategories.contains(category);
}

CategoryManager::EmbeddedCategory CategoryManager::embeddedCategoryType(Category *category) const
{
	for(int i = In; i <= LastCategory; ++i)
	{
		if (m_embeddedCategories[i] == category)
			return CategoryManager::EmbeddedCategory(i);
	}

	return CategoryManager::EmbeddedCategoryCount;
}

Database::Categories CategoryManager::embeddedCategories() const
{
	return m_embeddedCategories;
}

Database::Categories CategoryManager::optionalCategories() const
{
	Database::Categories res = m_database->categories();

	foreach(Category* category, m_embeddedCategories)
		res.removeOne(category);

	return res;
}

Category* CategoryManager::getTaskEmbeddedCategory(const Task *task) const
{
	Database::Categories categories = task->categories();
	foreach(Category* category, categories)
		if (m_embeddedCategories.contains(category))
			return category;
	return 0;
}

Database::Categories CategoryManager::getTaskOptionalCategories(const Task *task) const
{
	Database::Categories categories = task->categories();

	for(int i = 0; i < categories.count();)
		if (m_embeddedCategories.contains(categories[i]))
			categories.removeAt(i);
		else
			i++;

	return categories;
}

}
